#include <iostream>
using std::cout;
using std::cin;
using std::endl;
class Singleton {
public://公有成员函数
    static Singleton* getInstance() {
        if (ms_instance == nullptr) {
            ms_instance = new Singleton{};
        }
        return ms_instance;
    }
private://私有嵌套类
    class AutoRelease {
        friend class Singleton;
    private:
        AutoRelease() {
            cout << "AutoRelease()" << endl;
        }
        ~AutoRelease() {
            if (ms_instance) {
                delete ms_instance;
                ms_instance = nullptr;
                cout << "Release over" << endl;
            }
        }
    };//end of class AutoRelease
private://私有成员函数
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;
    Singleton(int val = 1)
        :m_val(val)
    {
        cout << " Singleton(int val)" << endl;
    }
    ~Singleton() {
        cout << " ~Singleton()" << endl;
    }
private://私有数据成员
    int m_val;
    static AutoRelease ms_autorelease;
    static Singleton* ms_instance;

};//end of class Singleton
//静态成员初始化
Singleton::AutoRelease Singleton::ms_autorelease;
Singleton* Singleton::ms_instance = nullptr;


void test() {
    cout << Singleton::getInstance() << endl;
    cout << Singleton::getInstance() << endl;
    cout << Singleton::getInstance() << endl;
}
int main() {
    cout << "before" << endl;
    test();
    cout << "after" << endl;
    return 0;
}